package com.xw.floatlib;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.xw.floatlib.callback.MusicCallBack;
import com.xw.floatlib.view.PlayerStateData;

import java.util.ArrayList;
import java.util.List;

public class MusicActivity extends AppCompatActivity implements MusicCallBack, View.OnClickListener {

    private SeekBar seekBar;
    private List<MusicData> musicDataList = new ArrayList<>();
    private TextView title_music;
    private boolean isPlaying = true;
    private ImageView imagePause;
    private ImageView imageLast;
    private ImageView imageNext;
    private ImageView imageCancle;
    private TextView tvProgress;
    private TextView tvLenght;
    private int maxLength = 0;
    private RelativeLayout relat_view;
    private ImageView image_icon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);

        getSupportActionBar().hide();
        findId();

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.float_music_bg);
        Tools.initTitleBarBitMap(this,relat_view,bitmap);

        FloatActionController.getInstance().hide();
        FloatMonkService.registerCallback("0",this);

        musicDataList = (List<MusicData>) getIntent().getSerializableExtra("music_list");
        int postion = getIntent().getIntExtra("music_position",0);

        isPlaying = getIntent().getBooleanExtra("playState",false);
        int progress = getIntent().getIntExtra("progress",0);

        title_music.setText(musicDataList.get(postion).getTitle());
        imagePause.setImageResource(isPlaying ? R.drawable.ic_float_music_stop : R.drawable.ic_float_music_start);
        seekBar.setProgress(progress);
        setClick();

    }

    private void findId() {
        seekBar = findViewById(R.id.seekbar);
        title_music = findViewById(R.id.title_music);
        relat_view = findViewById(R.id.relat_view);
        image_icon = findViewById(R.id.image_icon);

        imageLast = findViewById(R.id.image_last);
        imagePause = findViewById(R.id.image_pause);
        imageNext = findViewById(R.id.image_nest);
        imageCancle = findViewById(R.id.image_cancle);

        tvProgress = findViewById(R.id.text_progress);
        tvLenght = findViewById(R.id.text_lenght);

    }

    private void setClick() {
        imageLast.setOnClickListener(this);
        imagePause.setOnClickListener(this);
        imageNext.setOnClickListener(this);
        imageCancle.setOnClickListener(this);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser){
                    FloatActionController.getInstance().updataProgress(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }


    @Override
    public void updateProgress(PlayerStateData stateData) {
        seekBar.setProgress(stateData.getProgress());
        tvProgress.setText(Tools.getMusicTime(stateData.getProgress()));

        if (maxLength != stateData.getMaxLength()) {
            maxLength = stateData.getMaxLength();
            seekBar.setMax(maxLength);
            tvLenght.setText(Tools.getMusicTime(maxLength));
            title_music.setText(stateData.getAudioName());
        }

    }

    @Override
    public void MusicPlatState(boolean isPlaying) {

    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.image_last) {
            FloatActionController.getInstance().lastMusic();
            imagePause.setImageResource(R.drawable.ic_float_music_stop);
            isPlaying = true;
        }else if (v.getId() == R.id.image_pause){
            FloatActionController.getInstance().stop2Start();
            if (isPlaying) {
                imagePause.setImageResource(R.drawable.ic_float_music_start);
                isPlaying = false;
            }else{
                imagePause.setImageResource(R.drawable.ic_float_music_stop);
                isPlaying = true;
            }

        }else if (v.getId() == R.id.image_nest){
            FloatActionController.getInstance().nextMusic();
            imagePause.setImageResource(R.drawable.ic_float_music_stop);
            isPlaying = true;
        }else if(v.getId() == R.id.image_cancle){
            FloatMonkService.unRegister("0");
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FloatActionController.getInstance().show();
    }
}
